package utils

import (
	"fmt"
	"reflect"
)

// CopyProperties 复制属性值
func CopyProperties(src interface{}, target interface{}) {
	sval := reflect.ValueOf(src).Elem()
	dval := reflect.ValueOf(target).Elem()

	for i := 0; i < sval.NumField(); i++ {
		value := sval.Field(i)
		name := sval.Type().Field(i).Name
		dvalue := dval.FieldByName(name)
		if !dvalue.IsValid() {
			// 判断是否为nil
			continue
		}
		fmt.Printf("%s: %v\n", name, dvalue.CanConvert(value.Type()))
		if dvalue.CanConvert(value.Type()) {
			dvalue.Set(value)
		}
	}
}
